package com.itheima.health.common;

import com.alibaba.druid.util.StringUtils;
import org.apache.ibatis.annotations.Mapper;

import javax.sound.midi.Soundbank;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {


    /**
     * 获取某年某月的第一天
     * @Title:getFisrtDayOfMonth
     * @Description:
     * @param:@param year
     * @param:@param month
     * @param:@return
     * @return:String
     * @throws
     */
    public static String getFisrtDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最小天数
        int firstDay = cal.getActualMinimum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最小天数
        cal.set(Calendar.DAY_OF_MONTH, firstDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String firstDayOfMonth = sdf.format(cal.getTime());
        return firstDayOfMonth;
    }

    /**
     * 获取某月的最后一天
     * @Title:getLastDayOfMonth
     * @Description:
     * @param:@param year
     * @param:@param month
     * @param:@return
     * @return:String
     * @throws
     */
    public static String getLastDayOfMonth(int year,int month)
    {
        Calendar cal = Calendar.getInstance();
        //设置年份
        cal.set(Calendar.YEAR,year);
        //设置月份
        cal.set(Calendar.MONTH, month-1);
        //获取某月最大天数
        int lastDay = cal.getActualMaximum(Calendar.DAY_OF_MONTH);
        //设置日历中月份的最大天数
        cal.set(Calendar.DAY_OF_MONTH, lastDay);
        //格式化日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String lastDayOfMonth = sdf.format(cal.getTime());
        return lastDayOfMonth;
    }


    /**
     * String类型转Date类型，标准格式：yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static Date stringToDate(String dateStr) {
        if (StringUtils.isEmpty(dateStr)){
            throw new RuntimeException("日期字符串为空");
        }
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        Date date;
        try {
            date = sdf.parse(dateStr);
        }catch (ParseException e){
            throw new RuntimeException("字符串转日期异常,字符串："+dateStr);
        }
        return date;
    }
    //获取当前日期
    public static String showDateNow(Date date) {
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String format = simpleDateFormat.format(date);//时间格式转换
        return format;
    }

    public static void main(String[] args) {
        String fisrtDayOfMonth = DateUtil.getFisrtDayOfMonth(2022, 2);
        String lastDayOfMonth = DateUtil.getLastDayOfMonth(2202, 2);
        System.out.println(fisrtDayOfMonth+"----"+lastDayOfMonth);
        Date date=new Date();
        String s = showDateNow(date);
        System.out.println("------------------");
        System.out.println(date.getTime());
        System.out.println(s);
        Date date1 = stringToDate(s);
        long time = date1.getTime();

        System.out.println("********************");
        System.out.println(new Date().getTime());
        System.out.println(time);

        System.out.println(date==date1);

    }
}
